package com.itheima.manager;

import com.itheima.domain.Car;

import java.util.ArrayList;
import java.util.Scanner;

public class CarManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Car> list = new ArrayList<>();
        //定义用户名和密码
        String userName = "admin";
        String passWord = "12345";
        System.out.println("------------欢迎使用老马头汽车管理系统-----------");
        //三次录入机会
        for (int i = 0; i < 3; i++) {
            System.out.println("请输正确的登录账号");
            String uName = sc.next();
            System.out.println("请输入正确的密码");
            String pWord = sc.next();
            //录入的用户名和密码来进行比较
            if (userName.equals(uName) && passWord.equals(pWord)) {
                //登陆成功进入主菜单
                while (true) {
                    System.out.println("1 添加 车辆信息");
                    System.out.println("2 删除 车辆信息");
                    System.out.println("3 修改 车辆信息");
                    System.out.println("4 查看 车辆信息");
                    System.out.println("5 退出");
                    System.out.println("请输入您的选择:");
                    String choice = sc.next();

                    switch (choice) {
                        case "1":
                            //System.out.println("添加功能");
                            addCar(list);
                            break;
                        case "2":
                            //System.out.println("删除功能");
                            deleteCar(list);
                            break;
                        case "3":
                            //System.out.println("修改功能");
                            updateCar(list);
                            break;
                        case "4":
                            // System.out.println("查看功能");
                            queryCar(list);
                            break;
                        case "5":
                            System.out.println("感谢您的使用");
                            System.exit(0);
                        default:
                            System.out.println("您的输入有误");
                            break;
                    }
                }
                //登录三次失败直接结束掉虚拟机
            } else if (i == 2) {
                System.out.println("用户登录三次失败,您不配再拥有");
                System.exit(0);
            } else {
                //提示还有几次输入机会
                System.out.println("用户名密码错误还有"+(2-i)+"次机会");
            }
        }

    }

    /**
     * 修改功能
     *
     * @param list
     */
    private static void updateCar(ArrayList<Car> list) {
        System.out.println("请输入您要修改的车辆id");
        Scanner sc = new Scanner(System.in);
        String updateCar = sc.next();
        int index = getIndex(list, updateCar);
        if (index == -1) {
            System.out.println("查无车辆信息,请添加后再进行操作");
        } else {
            System.out.println("请输入车辆品牌");
            String brand = sc.next();
            System.out.println("请输入车辆型号");
            String type = sc.next();
            System.out.println("请输入车辆颜色");
            String color = sc.next();
            System.out.println("请输入车辆车牌");
            String numbers = sc.next();
            Car c = new Car(updateCar, brand, type, color, numbers);
            list.set(index, c);
            System.out.println("修改成功");
        }
    }

    /**
     * 删除
     *
     * @param list
     */
    private static void deleteCar(ArrayList<Car> list) {
        System.out.println("请输入您要删除的车辆id");
        Scanner sc = new Scanner(System.in);
        String deleteCar = sc.next();
        int index = getIndex(list, deleteCar);
        if (index == -1) {
            System.out.println("查无车辆信息,请添加后再进行操作");
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    /**
     * 查看功能
     *
     * @param list
     */
    private static void queryCar(ArrayList<Car> list) {
        if (list.size() == 0) {
            System.out.println("查无车辆信息请添加后查看");
        }
        System.out.println("编号\t品牌\t型号\t颜色\t车牌");
        for (int i = 0; i < list.size(); i++) {
            Car car = list.get(i);
            System.out.println(car.getId() + "\t" + car.getBrand() + "\t" + car.getType() + "\t\t" + car.getColor() + "\t\t" + car.getNumbers());
        }
    }

    /**
     * 添加功能
     *
     * @param list
     */
    private static void addCar(ArrayList<Car> list) {
        Scanner sc = new Scanner(System.in);
        //给出提示信息
        String id;
        while (true) {
            System.out.println("请输入车辆id");
            id = sc.next();
            int index = getIndex(list, id);
            if (index == -1) {
                break;
            }
        }
        System.out.println("请输入车辆品牌");
        String brand = sc.next();
        System.out.println("请输入车辆型号");
        String type = sc.next();
        System.out.println("请输入车辆颜色");
        String color = sc.next();
        System.out.println("请输入车辆车牌");
        String numbers = sc.next();
        Car c = new Car(id, brand, type, color, numbers);
        list.add(c);
        System.out.println("添加成功");
    }

    /**
     * 查询索引存在的位置
     *
     * @param list
     * @param id
     * @return
     */
    private static int getIndex(ArrayList<Car> list, String id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Car car = list.get(i);
            String carId = car.getId();
            if (id.equals(carId)) {
                index = i;
            }
        }
        return index;
    }
}
